import { Component, OnInit ,ChangeDetectorRef } from '@angular/core';
import { CommonService } from '../service/common.service'
import { StorageService } from '../service/storage.service'
import { EventService } from '../service/event.service'

//引入接受路由传值的模块
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-confirmorder',
  templateUrl: './confirmorder.page.html',
  styleUrls: ['./confirmorder.page.scss'],
})
export class ConfirmorderPage implements OnInit {

  public order:any;
  public userInfo:any;
  public num:number;
  public total:any = 0;
  
  public show:boolean=false;
  public pwflag:boolean = false
  

  constructor(private common:CommonService,public activatedRoute: ActivatedRoute,private storage:StorageService,
    private changeDetectorRef:ChangeDetectorRef,private router:Router,private eventService:EventService) { }

  ngOnInit() {

    //接受路由传值过来的图片路径
    this.activatedRoute.queryParams.subscribe((data: any) => {
      this.order = JSON.parse(data.data)
      console.log(this.order)
    })

    this.userInfo = this.common.getInfo()


    //监听 信息缓存 事件
    this.eventService.event.on('usercoin',()=>{
      console.log(2222222222222222222222)
      // 传ID更新储存
      var api = '/tbr/getUserInfo'
      var params = {'userId': this.common.getInfo().userId}
      
      this.common.post(api,params,false).then((response:any)=>{
        if(response.code == 200){
          // 保存信息
          this.storage.set('PersonalInfo',response.data)
          this.userInfo = this.common.getInfo()
        }
      })
    })

  }

  // 确认购买
  confirm(){
    if(this.num > 0 && this.num !== null){
      this.total = (+this.order.productUnitPrice) * this.num
      this.total = this.total.toFixed(2)
    }else{
      this.common.presentToast("请输入正确的数量")
      return false
    }
    this.pwflag = true
    this.changeDetectorRef.detectChanges()
  }


  // 取消
  closepw(){
    this.pwflag = false
    this.changeDetectorRef.detectChanges()
  }

  // 购买数量
  onChange(){
    // console.log(this.num)
    if(this.num > 0 && this.num !== null){
      this.total = (+this.order.productUnitPrice) * this.num
      this.total = this.total.toFixed(2)
    }
  }
  // 确认
  onCodeCompleted(code: string){
    if(code == null || code == ''){
      this.common.presentAlert("密码不能为空")
      return false
    } 
    if(code.length < 6){
      this.common.presentAlert("密码不能少于6位数")
      return false
    } 
    
    this.show=true
    this.pwflag=false

    var api = '/tbr/submitOrder'
    var params = {
      coinType:this.order.coinType,        //用户id
      productId:this.order.id,    //产品编号
      paymentCode:code,  //支付密码
      storage:this.num,
      price:this.total
    }
    this.common.postbodys(api,params).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
        this.show=false
        this.pwflag = false
        code = ''
        this.num = null
        // this.userInfo.coin = this.userInfo.coin - this.total
        // 保存信息
        // this.storage.set('PersonalInfo',this.userInfo)
        this.event()
        this.router.navigate(['yes'])
      }else{
        // this.router.navigateByUrl('confirmorder/yes')
        // this.common.presentAlert(res.msg)
        this.pwflag = false
        this.show=false
        code = ''
        this.num = null
      }
      
    })
  }

  event(){
      // 通知用户中心更新用户信息
      this.eventService.event.emit('usercoin');
      this.eventService.event.emit('hashrate');
      this.eventService.event.emit('allearnings');
      
  }
}
